package com.xxxx.seckill.controller;

import com.xxxx.seckill.VO.LoginVO;
import com.xxxx.seckill.VO.ResponseBean;
import com.xxxx.seckill.service.IUserService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest; // 不可以用javax！
import jakarta.servlet.http.HttpServletResponse; // 不可以用javax！

/**
 * <p>
 * 登录
 * </p>
 * <p>
 * 只能用@Controller而不能用@RestController
 * 因为@RestController会默认给此类下所有方法加上response body
 * 这样就是返回对象，而不是做页面跳转
 *
 * @author Jack
 * @since 2024-01-20
 */
@Controller
@RequestMapping("/login")
@Slf4j // 输出日志
public class LoginController {

    @Autowired
    private IUserService userService;

    /**
     * 功能描述：跳转登录页面
     */
    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    /**
     * 功能描述：登录功能
     *
     * @param loginVO:
     * @param request:
     * @param response:
     * @return controller
     */
    @RequestMapping("/doLogin")
    @ResponseBody
    public ResponseBean doLogin(@Valid LoginVO loginVO, HttpServletRequest request, HttpServletResponse response) { // 只有加上@Valid，LoginVO的验证注解才会生效！
        System.out.println("1234567890");
        log.info("{}", loginVO);
        return userService.doLogin(loginVO, request, response);
    }
}
